﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;

namespace StatusPlan3.UC
{
    /// <summary>
    /// Interaction logic for MyPage.xaml
    /// </summary>
    public partial class MyPage : UserControl
    {
        ProjectGroups projectGroups;

        public MyPage()
        {
            InitializeComponent();
            populateFrame();
        }

public void populateFrame()
{
    BusyIndicator.BusyContent = "Innlesur dáta";
    BusyIndicator.IsBusy = true;
    BackgroundWorker worker = new BackgroundWorker();

    worker.DoWork += delegate(object s, DoWorkEventArgs args) { args.Result = StatusPlanData.statusPlanHandler.getProjectGroups(StatusPlanData.currentUser.id,true); };
    worker.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    worker.RunWorkerAsync();
}

private void populateFrame2()
{
    Tasks tasks = new Tasks();
    Issues issues = new Issues();

    foreach (ProjectGroup pg in projectGroups)
    {
        foreach (Project p in pg.projects)
        {
            foreach (Task t in p.tasks)
            {
                tasks.Add(t);
            }

            foreach (Issue i in p.issues)
            {
                issues.Add(i);
            }

            foreach (Subproject sp in p.subproject)
            {
                foreach (Task t2 in sp.tasks)
                {
                    tasks.Add(t2);
                }

                foreach (Issue i2 in sp.issues)
                {
                    issues.Add(i2);
                }
            }
        }
    }
    TextBlock_UserName.Text = StatusPlanData.currentUser.fullName;
    TextBlock_Email.Text = StatusPlanData.currentUser.email;

    foreach (Role r in StatusPlanData.currentUser.roles)
    {
        if (TextBlock_Roles2.Text.Length > 0)
            TextBlock_Roles2.Text += ", ";
        TextBlock_Roles2.Text += r.name;
    }


    taskGridView.ItemsSource = tasks;

    issueGridView.ItemsSource = issues;

}

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    projectGroups = e.Result as ProjectGroups;
    BusyIndicator.IsBusy = false;
    populateFrame2();
}


    }
}
